'use strict';

angular.module('fbdApp.auxModule')
  .filter('highlightFilter', function() {
    return function(array, fullText) {

      var wrapByTag = function(input) {
        var str = '<span class="highlight">' + input + '</span>';
        return str;
      }

      var res = '';
      if (array.length == 0) {
        res = fullText;
      }
      for (var i = 0; i < array.length; i++) {
        if (i == 0) {
          res += fullText.substr(0, array[i].location[0]);
        } else {
          res += fullText.slice(array[i - 1].location[0] + array[i - 1].location[
            1], array[i].location[
            0]);
        }
        var match = fullText.substr(array[i].location[0], array[i].location[
          1]);
        res += wrapByTag(match);
        if (i == array.length - 1) {
          res += fullText.substr(array[i].location[0] + array[i].location[1]);
        }
      }
      return res;
    }
  });
